package com.woniuxy.carlife.carlifecommonsweb.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zjq
 * @ClassName:CopyUtil
 * @Description:CopyUtil
 * @date 2021/4/6 13:54
 **/

public class CopyUtil {
    /**
     * 功能描述〈集合复制〉
     *
     * @throws
     * @author:zjq
     * @param: [list, type]
     * @return: java.util.List<T>
     * @Date 2021/4/6 14:00
     */

    public static <T> List<T> getList(List list, Class<T> type) {
        ArrayList<T> newList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            try {
                T obj = type.newInstance();
                BeanUtils.copyProperties(list.get(i), obj);
                newList.add(obj);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }
        return newList;
    }


    /**
     * 功能描述〈单个对象复制〉
     *
     * @throws
     * @author:zjq
     * @param: [object, type]
     * @return: T
     * @Date 2021/4/6 14:00
     */

    public static <T> T getOne(Object object, Class<T> type) {
        T obj = null;
        try {
            obj = type.newInstance();
            BeanUtils.copyProperties(object, obj);

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
    }
}
